package com.cszjzx.member.action;

import com.cszjzx.member.service.MembercodeService;
import com.cszjzx.member.service.MembercodeServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class MemberCodeAction extends ActionSupport{
	private String oldStart;
	private String oldStop;
	private String newStart;
	private String newStop;
	private String nums;
	private String message;
	private MembercodeService membercodeService = new MembercodeServiceImpl();
	
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getOldStart() {
		return oldStart;
	}
	public void setOldStart(String oldStart) {
		this.oldStart = oldStart;
	}
	public String getOldStop() {
		return oldStop;
	}
	public void setOldStop(String oldStop) {
		this.oldStop = oldStop;
	}
	public String getNewStart() {
		return newStart;
	}
	public void setNewStart(String newStart) {
		this.newStart = newStart;
	}
	public String getNewStop() {
		return newStop;
	}
	public void setNewStop(String newStop) {
		this.newStop = newStop;
	}
	public String getNums() {
		return nums;
	}
	public void setNums(String nums) {
		this.nums = nums;
	}
	
	
	public String show(){
		String[] s = membercodeService.getStartAndStop();
		if(s == null){
			oldStart = "无";
			oldStop = "无";
			nums = "无";
		}
		else{
			oldStart = s[0];
			oldStop = s[1];
			char[] a = new StringBuffer(oldStop).reverse().toString().toCharArray();  
	        char[] b = new StringBuffer(oldStart).reverse().toString().toCharArray();  
	        int lenA = a.length;  
	        int lenB = b.length;  
	        // 找到最大长度  
	        int len = lenA > lenB ? lenA : lenB;  
	        int[] result = new int[len];  
	        // 表示结果的正负  
	        char sign = '+';  
	        // 判断最终结果的正负  
	        if (lenA < lenB) {  
	            sign = '-';  
	        } else if (lenA == lenB) {  
	            int i = lenA - 1;  
	            while (i > 0 && a[i] == b[i]) {  
	                i--;  
	            }  
	            if (a[i] < b[i]) {  
	                sign = '-';  
	            }  
	        }  
	        // 计算结果集，如果最终结果为正，那么就a-b否则的话就b-a  
	        for (int i = 0; i < len; i++) {  
	            int aint = i < lenA ? (a[i] - '0') : 0;  
	            int bint = i < lenB ? (b[i] - '0') : 0;  
	            if (sign == '+') {  
	                result[i] = aint - bint;  
	            } else {  
	                result[i] = bint - aint;  
	            }  
	        }  
	        // 如果结果集合中的某一位小于零，那么就向前一位借一，然后将本位加上10。其实就相当于借位做减法  
	        for (int i = 0; i < result.length - 1; i++) {  
	            if (result[i] < 0) {  
	                result[i + 1] -= 1;  
	                result[i] += 10;  
	            }  
	        }  
	  
	        StringBuffer sb = new StringBuffer();  
	        // 如果最终结果为负值，就将负号放在最前面，正号则不需要  
	        if (sign == '-') {  
	            sb.append('-');  
	        }  
	        // 判断是否有前置0  
	        boolean flag = true;  
	        for (int i = len - 1; i >= 0; i--) {  
	            if (result[i] == 0 && flag) {  
	                continue;  
	            } else {  
	                flag = false;  
	            }  
	            sb.append(result[i]);  
	        }  
	        // 如果最终结果集合中没有值，就说明是两值相等，最终返回0  
	        if (sb.toString().equals("")) {  
	            sb.append("0");  
	        }
			nums = new String(sb);
		}
		return "showSuccess";
	}
	
	public String edit(){
		char[] a = new StringBuffer(newStop).reverse().toString().toCharArray();  
        char[] b = new StringBuffer(newStart).reverse().toString().toCharArray();  
        int lenA = a.length;  
        int lenB = b.length;  
        // 找到最大长度  
        int len = lenA > lenB ? lenA : lenB;  
        int[] result = new int[len];  
        // 表示结果的正负  
        char sign = '+';  
        // 判断最终结果的正负  
        if (lenA < lenB) {  
            sign = '-';  
        } else if (lenA == lenB) {  
            int i = lenA - 1;  
            while (i > 0 && a[i] == b[i]) {  
                i--;  
            }  
            if (a[i] < b[i]) {  
                sign = '-';  
            }  
        }  
        // 计算结果集，如果最终结果为正，那么就a-b否则的话就b-a  
        for (int i = 0; i < len; i++) {  
            int aint = i < lenA ? (a[i] - '0') : 0;  
            int bint = i < lenB ? (b[i] - '0') : 0;  
            if (sign == '+') {  
                result[i] = aint - bint;  
            } else {  
                result[i] = bint - aint;  
            }  
        }  
        // 如果结果集合中的某一位小于零，那么就向前一位借一，然后将本位加上10。其实就相当于借位做减法  
        for (int i = 0; i < result.length - 1; i++) {  
            if (result[i] < 0) {  
                result[i + 1] -= 1;  
                result[i] += 10;  
            }  
        }  
  
        StringBuffer sb = new StringBuffer();  
        // 如果最终结果为负值，就将负号放在最前面，正号则不需要  
        if (sign == '-') {  
            sb.append('-');  
        }  
        // 判断是否有前置0  
        boolean flag = true;  
        for (int i = len - 1; i >= 0; i--) {  
            if (result[i] == 0 && flag) {  
                continue;  
            } else {  
                flag = false;  
            }  
            sb.append(result[i]);  
        }  
        // 如果最终结果集合中没有值，就说明是两值相等，最终返回0  
        if (sb.toString().equals("")) {  
            sb.append("0");  
        }
        if(sb.charAt(0) != '0' && sb.charAt(0) != '-'){
			membercodeService.updateStartAndStop(newStart, newStop);
			message = "团员编码修改成功";
		}
		else{
			message = "团员编码修改失败";
		}
		String[] s = membercodeService.getStartAndStop();
		if(s == null){
			oldStart = "无";
			oldStop = "无";
			nums = "无";
		}
		else{
			oldStart = newStart;
			oldStop = newStop;
			nums = new String(sb);
		}
		/*if(Integer.parseInt(newStart) <= Integer.parseInt(newStop)){
			membercodeService.updateStartAndStop(newStart, newStop);
			message = "团员编码修改成功";
		}
		else{
			message = "团员编码修改失败";
		}
		String[] s = membercodeService.getStartAndStop();
		if(s == null){
			oldStart = "无";
			oldStop = "无";
			nums = "无";
		}
		else{
			oldStart = s[0];
			oldStop = s[1];
			int aa = Integer.parseInt(s[1]) - Integer.parseInt(s[0]);
			nums = Integer.toString(aa);
		}*/
		return "showSuccess";
	}
}
